package com.hxunicorn.btc.common.utils;

import com.alibaba.fastjson.JSONObject;
import com.hxunicorn.btc.common.config.RptConfig;
import com.hxunicorn.common.exception.HxException;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * @author 墨塵
 * @since 2025/3/6
 */
@Log4j2
public class RptUtil {

    /**
     * token地址
     */
    private static final String TOKEN_URL = "http://61.48.133.11:9067/api/auth/token";

    /**
     * report地址
     */
    private static final String RPT_URL = "http://61.48.133.11:9067/api/metrics/industrial";


    /**
     * 获取token
     *
     * @return
     */
    public static JSONObject getAccessToken() {
        if (StringUtils.isBlank(RptConfig.user_name) || StringUtils.isBlank(RptConfig.pass_word)) {
            throw new HxException("未配置上报参数信息");
        }
        JSONObject param = new JSONObject();
        param.put("username", RptConfig.user_name);
        param.put("password", RptConfig.pass_word);
        HttpHeaders requestHeaders = new HttpHeaders();
        HttpEntity<JSONObject> formEntity = new HttpEntity<>(param, requestHeaders);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<JSONObject> response = restTemplate.postForEntity(TOKEN_URL, formEntity, JSONObject.class);
        return response.getBody();
    }

    /**
     * 上报数据
     *
     * @param param
     * @return
     */
    public static JSONObject rptData(JSONObject param) {
        JSONObject result = new JSONObject();
        String token = RptUtil.getAccessToken().get("token").toString();

        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.add("Content-Type", "application/json");
        requestHeaders.add("Authorization", "Bearer " + token);

//        JSONObject valueObj = new JSONObject();
//        valueObj.put("industrial_device_count", 0);
//        valueObj.put("valid_data_points", 120);
//        valueObj.put("cloud_vcpu_cores", 160);
//        valueObj.put("total_data_volume", 160);
//        valueObj.put("industrial_model_count", 160);
//        valueObj.put("industrial_app_count", 160);
//        valueObj.put("industrial_app_subscriptions", 160);
//        valueObj.put("killer_app_subscriptions", 160);
//        valueObj.put("registered_users", 160);
//        valueObj.put("monthly_active_users", 160);
//        valueObj.put("developer_registrations", 160);
//        valueObj.put("platform_honors", "国家级示范平台,行业标杆");
//        valueObj.put("specialization_features", "智能诊断,预测性维护");

        HttpEntity<JSONObject> formEntity = new HttpEntity<>(param, requestHeaders);
        RestTemplate restTemplate = new RestTemplate();
        try {
            ResponseEntity<JSONObject> response = restTemplate.postForEntity(RPT_URL, formEntity, JSONObject.class);
            result = response.getBody();
        } catch (Exception e) {
            result.put("errorMessage", e.getMessage());
        }

        return result;
    }
}
